<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>规则集管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		/* $(document).ready(function() {
			//$("#name").focus();
			  $("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				}, 
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
		}); */
		
    function delectDiv(id){
	    var num=$("#sel_num").val();
	    if(num==1){
	       alert("规则配置不能少于一条!");
	       return false;
	    }
    	var div=document.getElementsByName("rul_config");
    	var dlen=div.length;
    	var dd=div[dlen-1].id;
    	var di=div[dlen-2].id;
    	if(id==dd){    		
    		 $("#"+id).remove();
      	     $("#sel_num").val(num-1);
             $("#a"+di).after(" <a id='aa"+dd+"' href='javaScript:void(0);' onclick='addDiv("+dd+")' style='color:blue;'>再添加一条</a>");    
    	  }else{
    	   	 $("#"+id).remove();
      	     $("#sel_num").val(num-1);
       	}
     }
    
    function addDiv(id){
    	 var idd=id+1;
    	 var num=$("#sel_num").val();
    	 var nu=parseInt(num);
    	 $("#aa"+id).remove();
    	 $("#sel_num").val(nu+1);
        $("#d_div").append("<p><div class='controls;div-left;' name='rul_config' id='"+idd+"'>"+
		       "<select  name='rules["+id+"].keyName' class='input-xlarge required' id='sel_name"+idd+"' onchange=isCheck('"+idd+"')>"+
		         "<c:forEach items='${keyword}' var='key' varStatus='i' >"+
		          "<option value='${key.id}+${key.keywordname}+${key.type}'>${key.keywordname}</option>"+
		          "</c:forEach>"+
	           "</select>"+
	           "    <select name='rules["+id+"].nexus' class='input-xlarge required' id='sel_nexus"+idd+"'>"+
	                "<option  value='1'>>=</option>"+
				    "<option  value='2'>&lt;=</option>"+
				    "<option  value='3'>=</option>"+
				    "<option  value='4'>包含</option>"+
				    "<option  value='5'>不包含</option>"+
	           "<select>"+
	           "  <input name='rules["+id+"].ruleSet' htmlEscape='false' maxlength='20' class='input-xlarge required' id='sel_ruleSet"+idd+"'/>"+
	           "  <a id='a"+idd+"' href='javaScript:void(0);' onclick='delectDiv("+idd+")' style='color:blue;'>删除</a>"+
	           "  <a id='aa"+idd+"' href='javaScript:void(0);' onclick='addDiv("+idd+")' style='color:blue;'>再添加一条</a>"+
	           "  <span class='help-inline' id='sp_rul_set"+idd+"'><font color='red'>*</font> </span>"+
               "</div></P>");
          initRuleConfig(idd);   	  
        } 
    
    function preview(){
    	var rel=$("#sel_relation").val();
    	var num=$("#sel_num").val();
    	var re = getRelation(rel);
    	var str='';    	
    	var div=document.getElementsByName("rul_config");
    	var dlen=div.length;
    	for(var i=0;i<dlen;i++){
    		var id=div[i].id;
    		var name=$("#sel_name"+id).val();
			var nexus=$("#sel_nexus"+id).val();
			var remark=$("#sel_ruleSet"+id).val();
			var na=name.split('+');
			var nex=getNexus(nexus);
			if(nex==0){
			   str+=na[1]+remark; 
			 }else{
			   str+=na[1]+nex+remark;
			 }
			 if(i!=dlen-1){
				str=str+","+"\n"+re+"\n"; 	
			  }  
    	}
    	$("#input_remarks").val(str);
    }
    
    function getNexus(str){
    	if(str==1){
			str=">=";
		}else if(str==2){
			str="<=";
		}else if(str==3){
			str="="
		}else if(str==4){
			str="包含"
		}else if(str==5){
			str="不包含"
		}
    	
    	return str;
    }
    
    
    function getRelation(str){
    	 if(str==1){
    		 str="或者";
    	 }else if(str==2){
    		 str="并且";
    	 }
    	 
    	 return str;
    }
    
    $(function(){  
       var ruleCon='${aegisRuleSet.ruleconfig}';
       var len =eval(ruleCon).length;
       var rul=eval(ruleCon);
       $("#sel_num").val(len);
       if(ruleCon!=''){
    	   if(len>1){
    		  setRuleConfig(rul);
    		  for(i=1;i<len;i++){
    	           var j=i+1;
   			        $("#d_div").append("<p><div class='controls;div-left;' name='rul_config'   id='"+j+"'>"+
   				       "<select  name='rules["+i+"].keyName' class='input-xlarge required' id='sel_name"+j+"' onchange=isCheck('"+j+"')>"+
   				         "<c:forEach items='${keyword}' var='key' varStatus='i' >"+
   				          "<option value='${key.id}+${key.keywordname}+${key.type}'>${key.keywordname}</option>"+
   				          "</c:forEach>"+
   			           "</select>"+
   			           "    <select name='rules["+i+"].nexus' class='input-xlarge required' id='sel_nexus"+j+"'>"+
   			                "<option  value='0'></option>"+
   			                "<option  value='1'>>=</option>"+
   						    "<option  value='2'>&lt;=</option>"+
   						    "<option  value='3'>=</option>"+
   						    "<option  value='4'>包含</option>"+
   						    "<option  value='5'>不包含</option>"+
   			           "<select>"+
   			           "  <input name='rules["+i+"].ruleSet' htmlEscape='false' maxlength='20' class='input-xlarge required' id='sel_ruleSet"+j+"'/>"+
   			           "  <a id='a"+j+"' href='javaScript:void(0);' onclick='delectDiv("+j+")' style='color:blue;'>删除</a>"+
   			           "  <a id='aa"+j+"' href='javaScript:void(0);' onclick='addDiv("+j+")' style='color:blue;'>再添加一条</a>"+
   			           "  <span class='help-inline' id='sp_rul_set"+j+"'><font color='red'>*</font> </span>"+
   		               "</div></P>");
   			          $("#sel_name"+j).val(rul[i].keyId+"+"+rul[i].keyName+"+"+rul[i].keyType);
 			          $("#sel_nexus"+j).val(rul[i].nexus);
 			          $("#sel_ruleSet"+j).val(rul[i].ruleSet);
 			          $("#aa"+i).remove();
 			          isCheck(j);
    	             }
    	         }else if(len==1){
    	        	 setRuleConfig(rul);
    	         }
    	     preview();//调用预览
            }
       })
    
    
    function setRuleConfig(rul){
    	$("#d_div").html("<div class='controls;div-left;'  name='rul_config'    id='1'>"+
			       "<select  name='rules[0].keyName' class='input-xlarge required' id='sel_name1' onchange=isCheck(1)>"+
			         "<c:forEach items='${keyword}' var='key' varStatus='i' >"+
			          "<option value='${key.id}+${key.keywordname}+${key.type}'>${key.keywordname}</option>"+
			          "</c:forEach>"+
		           "</select>"+
		           "    <select name='rules[0].nexus' class='input-xlarge required' id='sel_nexus1'>"+
		                "<option  value='0'></option>"+
		                "<option  value='1'>>=</option>"+
					    "<option  value='2'>&lt;=</option>"+
					    "<option  value='3'>=</option>"+
					    "<option  value='4'>包含</option>"+
					    "<option  value='5'>不包含</option>"+
		           "<select>"+
		           "  <input name='rules[0].ruleSet' htmlEscape='false' maxlength='20' class='input-xlarge required' id='sel_ruleSet1'/>"+
		           "  <a id='a1' href='javaScript:void(0);' onclick='delectDiv(1)' style='color:blue;'>删除</a>"+
		           "  <a id='aa1' href='javaScript:void(0);' onclick='addDiv(1)' style='color:blue;'>再添加一条</a>"+
		           "  <span class='help-inline' id='sp_rul_set1'><font color='red'>*</font> </span>"+
	               "</div>");
 	     $("#sel_name1").val(rul[0].keyId+"+"+rul[0].keyName+"+"+rul[0].keyType);
		 $("#sel_nexus1").val(rul[0].nexus);
		 $("#sel_ruleSet1").val(rul[0].ruleSet);
		 isCheck(1);
    }
    
      function audit(status){
    	  if(confirm("确定作废规则集吗？")){
    	     window.location.href="${ctx}/rule/aegisRuleSet/audit?id=${aegisRuleSet.id}&status="+status+"&branchId=${branchId}";
    	  }
       }
      
      
      function isCheck(id){
    	  var s_name=$("#sel_name"+id).val();
    	  var type=s_name.split('+');
    	  
    	  if(type[2]==1){
    		  var nex=$("#sel_nexus"+id+" option:first").val();
    		  if(nex!=0){
    		    $("#sel_nexus"+id).append("<option value='0'></option>");
    		  }
    		  $("#sel_ruleSet"+id).val("");
    		  $("#sel_nexus"+id).val(0);
    		  $("#sel_nexus"+id).attr("disabled",true);
    		  $("#sel_ruleSet"+id).attr("readonly",true);
    	  }else{
    		  $("#sel_nexus"+id+" option[value='0']").remove();
    		  $("#sel_nexus"+id).attr("disabled",false);
    		  $("#sel_ruleSet"+id).attr("readonly",false);
    	  }
    	  
      }
      
    window.onload=function(){
    	  var id='${aegisRuleSet.id}';
    	  if(id==''){   		 
    	     $("#d_div").append("<div class='controls;div-left;' name='rul_config'  id='1'>"+
			       "<select  name='rules[0].keyName' class='input-xlarge required' id='sel_name1' onchange=isCheck(1)>"+
			         "<c:forEach items='${keyword}' var='key' varStatus='i' >"+
			          "<option value='${key.id}+${key.keywordname}+${key.type}'>${key.keywordname}</option>"+
			          "</c:forEach>"+
		           "</select>"+
		           "    <select name='rules[0].nexus' class='input-xlarge required' id='sel_nexus1'>"+
		                "<option  value='0'></option>"+
		                "<option  value='1'>>=</option>"+
					    "<option  value='2'>&lt;=</option>"+
					    "<option  value='3'>=</option>"+
					    "<option  value='4'>包含</option>"+
					    "<option  value='5'>不包含</option>"+
		           "<select>"+
		           "  <input name='rules[0].ruleSet' htmlEscape='false' maxlength='20' class='input-xlarge required' id='sel_ruleSet1'/>"+
		           "  <a id='a1' href='javaScript:void(0);' onclick='delectDiv(1)' style='color:blue;'>删除</a>"+
		           "  <a id='aa1' href='javaScript:void(0);' onclick='addDiv(1)' style='color:blue;'>再添加一条</a>"+
		           "  <span class='help-inline' id='sp_rul_set1'><font color='red'>*</font> </span>"+
	               "</div>");
    	     initRuleConfig(1);
         }
    }
      
    function  save(){
    	
    	if(!check()){
    		return false;
    	}
    	
    	if(!isExistRuleName()){
    		return false;
    	 }
    	
    	
      if(confirm("确定提交规则集吗？")){
    	document.forms[0].submit();
     }
     
    }
    
   
    
    function  check(){
    	var ruleName=$("#rul_name").val();
    	var re=/^\s+$/g;
    	if(ruleName.length == 0 || re.test(ruleName)){
    		$("#sp_rul_name").html("<font color='red'>规则集名称不能为空!</font>");
    		return false;
    	}else{
    		$("#sp_rul_name").html("<font color='red'>*</font>");
    	}
   
    	var div=document.getElementsByName("rul_config");
    	 var count=0;
    	for(i=0;i<div.length;i++){
    		 var id=div[i].id;
    		 var s_name=$("#sel_name"+id).val();
       	     var type=s_name.split('+');
       	     var rul_set=$("#sel_ruleSet"+id).val();
       	     if(type[2]==2&&rul_set==''){
       	    	$("#sp_rul_set"+id).html("<font color='red'>规则配置输入内容不能为空!</font>");
       	    	count++;
       	     }else{
       	       $("#sp_rul_set"+id).html("<font color='red'>*</font>");
       	     }       	    
    	}
    	    
    	if(count>0){
    		return false;
    	}
    	
    	return  true;
    	
    }
    
    
    function initRuleConfig(id){
    	 var k_name=$("#sel_name"+id+" option:first").val();
    	 var type=k_name.split('+');
    	 if(type[2]==1){
   		  var nex=$("#sel_nexus"+id+" option:first").val();
   		  if(nex!=0){
   		    $("#sel_nexus"+id).append("<option value='0'></option>");
   		  }
   		  $("#sel_nexus"+id).val(0);
   		  $("#sel_nexus"+id).attr("disabled",true);
   		  $("#sel_ruleSet"+id).attr("readonly",true);
   	    }else{
  		  $("#sel_nexus"+id+" option[value='0']").remove();
		  $("#sel_nexus"+id).attr("disabled",false);
		  $("#sel_ruleSet"+id).attr("readonly",false);
	  } 
    }
    
    
   function  isExistRuleName(){
	      var isExist = false;
	      var id='${aegisRuleSet.id}';
	      var branchId='${branchId}';
	      var ruleName=$("#rul_name").val();
	     //同步方法调用  判断规则集名称是否存在
		  $.ajax({
			    type: "POST",
			    url: "${ctx}/rule/aegisRuleSet/isExistRuleName",
			    data: {id:id,branchId:branchId,ruleName:ruleName},
			    dataType: "json",
			    async:false, 
			    success: function(data){
			    	  if(data){
			    		$("#sp_rul_name").html("<font color='red'>*</font>");
			    		isExist=true;
			    	  }else{
			    		$("#sp_rul_name").html("<font color='red'>规则集名称已经存在!</font>");
			    		isExist=false;
			    	 }
			   }
	
           })
        return isExist;
     }
    
    
    
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/rule/aegisRuleSet?branchId=${branchId}">规则集列表</a></li>
		<li class="active"><a href="${ctx}/rule/aegisRuleSet/form?id=${aegisRuleSet.id}">规则集<shiro:hasPermission name="rule:aegisRuleSet:edit">${not empty aegisRuleSet.id?'修改':'添加'}</shiro:hasPermission><shiro:lacksPermission name="rule:aegisRuleSet:edit">查看</shiro:lacksPermission></a></li>
	</ul><br/>
	<form:form id="inputForm" modelAttribute="aegisRuleSet" action="${ctx}/rule/aegisRuleSet/save?branchId=${branchId}" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<input type="hidden" id="sel_num" value="1"/>
		<sys:message content="${message}"/>		
		<div class="control-group">
			<label class="control-label">规则集名称：</label>
			<div class="controls">
				<form:input path="rulesetname" htmlEscape="false" maxlength="20" class="input-xlarge required" id="rul_name"/>
				<span class="help-inline" id="sp_rul_name"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">业务类型：</label>
			<div class="controls">
				<form:select path="businesstype" class="input-xlarge required">
					<form:option value="1" label="金币业务"/>
					<form:option value="2" label="其他业务"/>
				</form:select>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">子集关系：</label>
			<div class="controls">
				<form:select path="relation" class="input-xlarge required" id="sel_relation">
					<form:option  value="1" label="或者"/>
					<form:option  value="2" label="并且"/>
				</form:select>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group" id="d_rules">
			<label class="control-label">规则配置：</label>
			<p><div class="controls" id="d_div"></div></p>
		</div>
		<div class="control-group">
			<label class="control-label">结果预览</label>
			<div class="controls">
				<textarea name="remark" htmlEscape="false" rows="4" maxlength="500" class="input-xxlarge" readonly="true" id="input_remarks"></textarea>
			</div>
		</div>
		<div class="form-actions">
		     <input id="btnPre" class="btn btn-primary" type="button" value="预览" onclick="preview()"/>&nbsp;
		     <c:if test="${aegisRuleSet.id==null}">
			 <shiro:hasPermission name="rule:aegisRuleSet:edit"><input id="btnSubmit" class="btn btn-primary" type="button" value="保 存" onclick="save()"/>&nbsp;</shiro:hasPermission>
			 <input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
			 </c:if>
			 <c:if test="${aegisRuleSet.id!=null}">
			    <shiro:hasPermission name="rule:aegisRuleSet:edit"><input id="btnSubmit" class="btn btn-primary" type="button" value="重新提交"  onclick="save()"/>&nbsp;</shiro:hasPermission>
			    <shiro:hasPermission name="rule:aegisRuleSet:edit"><input id="btnSubmit" class="btn btn-primary" type="button" value="作废" onclick="audit('03')" />&nbsp;</shiro:hasPermission>
			 </c:if>
		</div>
	</form:form>
</body>
</html>